package com.resume.aipeople.controller;

import com.resume.aipeople.dto.CharacterDTO;
import com.resume.aipeople.service.CharacterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/characters")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin(origins = "*")
public class CharacterController {
    
    private final CharacterService characterService;
    
    /**
     * 获取所有活跃角色
     */
    @GetMapping
    public ResponseEntity<List<CharacterDTO>> getAllCharacters() {
        try {
            List<CharacterDTO> characters = characterService.getAllCharacters();
            return ResponseEntity.ok(characters);
        } catch (Exception e) {
            log.error("获取角色列表失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 搜索角色
     */
    @GetMapping("/search")
    public ResponseEntity<List<CharacterDTO>> searchCharacters(@RequestParam String keyword) {
        try {
            List<CharacterDTO> characters = characterService.searchCharacters(keyword);
            return ResponseEntity.ok(characters);
        } catch (Exception e) {
            log.error("搜索角色失败: {}", keyword, e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 获取热门角色
     */
    @GetMapping("/popular")
    public ResponseEntity<List<CharacterDTO>> getPopularCharacters() {
        try {
            List<CharacterDTO> characters = characterService.getPopularCharacters();
            return ResponseEntity.ok(characters);
        } catch (Exception e) {
            log.error("获取热门角色失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 获取所有角色分类
     */
    @GetMapping("/categories")
    public ResponseEntity<List<String>> getAllCategories() {
        try {
            List<String> categories = characterService.getAllCategories();
            return ResponseEntity.ok(categories);
        } catch (Exception e) {
            log.error("获取角色分类失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据分类获取角色
     */
    @GetMapping("/category/{category}")
    public ResponseEntity<List<CharacterDTO>> getCharactersByCategory(@PathVariable String category) {
        try {
            List<CharacterDTO> characters = characterService.getCharactersByCategory(category);
            return ResponseEntity.ok(characters);
        } catch (Exception e) {
            log.error("根据分类获取角色失败: {}", category, e);
            return ResponseEntity.internalServerError().build();
        }
    }
} 